package com.ysd.user.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ysd.common.entity.User;
import com.ysd.common.utils.Json;
import com.ysd.user.service.UserService;


@Controller
@RequestMapping("/front/")
@ResponseBody
public class FrontLoginController {
	
	

	@Autowired
	private UserService userService;
	
	/**
	 * 前台登录验证
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public Json login(String name,String password,HttpSession session) {
		User user = userService.frontLogin(name,password);
		if(user != null) {
			session.setAttribute("user", user);
		}
		return Json.success(user);
	}
	
	/**
	 * 前台用户注册
	 * @param password
	 * @return 布尔类型
	 */
	@RequestMapping("/addUser")
	public Boolean registerUser(User user) {
		return userService.addUser(user);
	}
	
	/**
	 * 退出登录，清除session中得用户信息
	 * @param session
	 */
	@RequestMapping("/loginOut")
	public String loginOut(HttpSession session) {
		session.removeAttribute("user");
		return "frontPage/login";
	}
	
	/**
	 * 修改编辑用户
	 * @param user 
	 */
	@RequestMapping("/editUserById")
	public Boolean editUserById( User user) {
		//获得传入的是否为管理员状态如是者传的值为on，再就行赋值为对应的1（是）0（不是）
		return userService.editUserById(user);
	}
	
	/**
	 * 查询用户
	 */
	@RequestMapping("/queryAllUser")
	public Json findUser( User user) {
		List<User> userList = userService.findUserByParm(user);
		return Json.success(userList);
	}
}
